package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ContactObjects;

public class ContactDataGenerator {
	
//	public static final String ANY = "any";
	Random rnd = new Random();
	
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts(){
		List<ContactObjects> contacts = new ContactDataGenerator().generateRandomContactList(5);		
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObjects contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();		
	}

	//read from file
	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException{
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			ContactObjects contact = new ContactObjects()
				.setFirstname(parts[0])
				.setLastname(parts[1])
				.setAddress(parts[2])
				.setHome(parts[3])
				.setMobile(parts[4])
				.setWork(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setBday(parts[8])
				.setBmonth(parts[9])
				.setByear(parts[10])
				.setGroup(parts[11])
				.setAddress2(parts[12])
				.setPhone2(parts[13]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}
	/**
    *
    * @param min The (included) lower bound of the range
    * @param max The (included) upper bound of the range
    *
    * @return The random value in the range
    */
   public static int randInRangeInc(int min, int max) {
           return min + (int) (Math.random() * (max - min));
   }
   
	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//write to file
	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObjects> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()){
			//file.delete();
			System.out.println("File exists, stop generator");
			return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObjects contact : contacts) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" + 
					contact.address + "\t" + contact.home + "\t" + contact.mobile + "\t" + 
					contact.work + "\t" + contact.email + "\t" + contact.email2 + "\t" + 
					contact.bday + "\t" + contact.bmonth + "\t" + contact.byear + "\t" + 
					contact.group + "\t" + contact.address2 + "\t" + contact.phone2 + "\n");
		}
		writer.close();
	}

	private List<ContactObjects> generateRandomContactList(int count) {
		List<ContactObjects> list = new ArrayList<ContactObjects>();
		for (int i = 0; i < count; i++) {
			ContactObjects contact = new ContactObjects()
				.setFirstname("firstname"+rnd.nextInt())
				.setLastname("lastname"+rnd.nextInt())
				.setAddress("address"+rnd.nextInt())
				.setHome("+7-812-"+rnd.nextInt(9999999))
				.setMobile("+7-812-"+rnd.nextInt(9999999))
				.setWork("+7-812-"+rnd.nextInt(9999999))
				.setEmail("email"+rnd.nextInt()+"@email.com")
				.setEmail2("email2"+rnd.nextInt()+"@email.com")
				.setBday(String.valueOf(rnd.nextInt(30)))
				.setBmonth(String.valueOf(rnd.nextInt(12)))
				.setByear(String.valueOf(randInRangeInc(1900, 2012)))
				.setGroup(String.valueOf(rnd.nextInt(3)))
				.setAddress2("address"+rnd.nextInt())
				.setPhone2("+7-812-"+rnd.nextInt(9999999));
			list.add(contact);
		}
		return list;		
	} 
}